/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-09-22 13:31:58
 * @version $Id$
 * 
 * 渲染消息
 * 添加单条消息
 * 
 * 
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports,typeof exports !== 'undefined' ? require('./zepto.js')&&root.$ : root.$, require('../../public_src/js/1_self/3_log.js'));

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports','./zepto.js','../../public_src/js/1_self/3_log.js'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    factory({}, root.$, root.log);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, $, log, undefined){
  'use strict';

  /*
    调试开关
  */
  log._logToggle(true);
  
  var myScroll = null;

  $('.jsRemark').on('click', function(event) {
    

    /*
      事件委托
        图片放大
    */

    /* Act on the event */

    var eventTarget = $(event.target).data('event');

    var IScroll = require('../../public_src/js/iscroll/build/iscroll.js');
    require('../../public_src/js/iscroll/build/iscroll-zoom.js');

    switch(eventTarget){
      case 'enImg':
        var img = new Image();

        var width = window.screen.width<=window.innerWidth? window.screen.width:window.innerWidth;
        var height = window.screen.height<=window.innerHeight? window.screen.height:window.innerHeight;

        img.onload = function(){
          $('.jsEnlarge').css({
            width : 'auto',
            height : 'auto'
          });
          $('.jsEnlarge').attr({
            'data-src' : $('.jsEnlarge').attr('src'),
            'src' : $(event.target).data('img')
          });

          /*
            图片的宽高比大于屏幕的宽高时，图片为小图，居中显示
            图片的宽高比小于屏幕的宽高时，图片为长图，置顶显示
          */
          if (img.width/img.height > width/height) {
            $('#scroller').addClass('img-enlarge');
          }else{
            $('#scroller').removeClass('img-enlarge');
          }

          log._log(parseFloat(img.width)*parseFloat($('html').css('fontSize'))/parseFloat(img.height) + 'rem');
          $('#scroller').height(parseFloat(img.width)*parseFloat($('html').css('fontSize')/parseFloat(img.height)) + 'rem');

          startScroll();

        };

        window.history.pushState('enlarge','图片放大后退','#/enlarge');
        window.addEventListener('popstate',backBtn);

        img.src = $(event.target).data('img');
        $('.jsEnlargeBg').removeClass('s-hide');
        $('.jsEnlarge').css({
          width : '27px',
          height : '27px'
        });

        function startScroll () {
          myScroll = new IScroll('#wrapper', {
            zoom: true,
            scrollX: true,
            scrollY: true,
            mouseWheel: true,
            tap: true,
            click: true,
            wheelAction: 'zoom'
          });
        }
        break;
      case 'close':
        imgBack({});

        break;
    }
  });

  function backBtn(e){
    backCallback(e,backBtn);
  }
  function imgBack(e){
    window.history.back();
  }
  function backCallback(e,backBtn){
    if (myScroll) {
      myScroll.destroy();
    }
    
    myScroll = null;

    $('.jsEnlargeBg').addClass('s-hide');
    $('.jsEnlarge').attr({
        'src' : $('.jsEnlarge').data('src')
    });
    $('.jsEnlarge').css({
      width : 'auto',
      height : 'auto'
    });
    $('#scroller').addClass('img-enlarge');
    
    log._log(e);
    window.removeEventListener('popstate',backBtn);
  
  }

});